###
 # @Description: mysql数据库备份
 # @Version: 2.0
 # @Autor: gang
 # @Date: 2020-09-24 15:42:40
 # @LastEditors: gang
 # @LastEditTime: 2020-09-24 16:31:26
### 

path="/usr/bin/mysql"
dbpath=/home/test/dbbackup
myuser=root
mypass=123456
socket=/var/lib/mysql/mysql.sock

# 登陆数据库命令
mycmd=`${path} -h 192.168.1.10 -u${myuser} -p${mypass} -P3306 -e "show databases;" | sed '1,2d' | egrep -v "mysql|performance_schema|skinrun_face|skinrun_agent|skinrun_store|skinthree"` 


# 定义备份数据库命令
mydump="mysqldump -h 192.168.1.10 -u${myuser} -p${mypass} -P3306 -S ${socket}"

# 监测数据库目录是否存在，否侧创建
[ ! -d "${dbpath}" ] && {
    mkdir ${dbpath}
}

for dbname in ${mycmd}
do
    echo ${dbname}
    ${mydump} ${dbname} | gzip > ${dbpath}/${dbname}_$(date +%F).sql.gz
done
